﻿<UserControl x:Class="CredentialsManager.Views.CreateUserView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:vw="clr-namespace:CredentialsManager.Views"
             xmlns:p="clr-namespace:CredentialsManager"             
             mc:Ignorable="d"
             Height="500" Width="300">
    <UserControl.Resources>
        <Style TargetType="{x:Type FrameworkElement}" x:Key="DisablePasswordStyle">
            <Setter Property="IsEnabled" Value="True" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding GeneratePassword}" Value="True">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="{x:Type Grid}" x:Key="DisableOnGeneratePasswordStyle">
            <Setter Property="Visibility" Value="Visible" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding GeneratePassword}" Value="True">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="350" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Grid.Column="0">
            <Label>User Name:</Label>
            <TextBox IsEnabled="{Binding IsUserNameEditable}" Text="{Binding UserName, UpdateSourceTrigger=PropertyChanged}" TabIndex="0" />            
            <Label>Password:</Label>
            <PasswordBox TabIndex="1"
                p:PasswordBoxHelper.Attach="True"  
                p:PasswordBoxHelper.Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                Style="{StaticResource DisablePasswordStyle}" />
            <Grid Style="{StaticResource DisableOnGeneratePasswordStyle}">
                <TextBlock 
                    DataContext="{Binding PasswordValidity}" 
                    Text="{Binding ErrorContent}" 
                    Style="{StaticResource ErrorMessageStyle}" />
            </Grid>
            <Label>Confirm Password:</Label>
            <PasswordBox TabIndex="2" 
                p:PasswordBoxHelper.Attach="True"  
                p:PasswordBoxHelper.Password="{Binding CheckPassword, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                Style="{StaticResource DisablePasswordStyle}"
                />
            <Grid Style="{StaticResource DisableOnGeneratePasswordStyle}">
                <TextBlock 
                    DataContext="{Binding CheckPasswordValidity}" 
                    Text="{Binding ErrorContent}" 
                    Style="{StaticResource ErrorMessageStyle}" />
            </Grid>                
            <Label>Email:</Label>
            <TextBox Text="{Binding Email, UpdateSourceTrigger=PropertyChanged}" TabIndex="3" />
            <Label>Security Question:</Label>
            <TextBox Text="{Binding SecurityQuestion, UpdateSourceTrigger=PropertyChanged}" 
                     TabIndex="4" IsEnabled="{Binding RequiresQuestionAndAnswer}" />
            <Label>Security Answer:</Label>
            <TextBox Text="{Binding SecurityAnswer, UpdateSourceTrigger=PropertyChanged}" 
                     TabIndex="5" IsEnabled="{Binding RequiresQuestionAndAnswer}" />
        </StackPanel>
        <Grid Grid.Column="0" Grid.Row="1" Margin="10" >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Label Grid.Row="0">Created Users</Label>
            <ListView Grid.Row="1" ItemsSource="{Binding NewUsers}" />
        </Grid>
        <StackPanel Grid.Row="0" Grid.Column="1" Margin="10">
            <CheckBox Content="Active User" IsChecked="{Binding IsApproved}" Margin="0, 0, 0, 10" />
            <CheckBox Content="Generate password" IsChecked="{Binding GeneratePassword}" />
            <Button 
                Content="Create User" 
                Command="{Binding CreateCommand}" 
                Margin="0, 10, 50, 10" 
                IsEnabled="{Binding IsValid}" 
                IsDefault="True" />
        </StackPanel>
        <WrapPanel 
            Grid.Row="1" 
            Grid.Column="1" 
            Margin="10" 
            HorizontalAlignment="Right" 
            VerticalAlignment="Bottom">
            <Button 
                    Command="{Binding CloseCommand}" 
                    IsCancel="True"
                    Content="Close" Padding="10, 5, 10, 5" />
        </WrapPanel>
    </Grid>
</UserControl>
